import router from "@/router/index";
import { useUserStore } from "@/store/auth";
router.beforeEach((to) => {
  const userStore = useUserStore();
  const isLogin = userStore.token;
  if (!isLogin) {
    //未登录
    if (to.path != "/login") {
      return router.push("/login");
    }
  } else {
    // 登录
    if (to.path === "/login") {
      return router.push("/");
    }
    //判断用户是否有权限访问相关路由,
    if (
      to.meta.needAuth &&
      userStore.roles.some((role: string) => {
        (to.meta.needAuth as string[]).includes(role)
      })
    ) {
      return { path: "/" };
    }
  }
});
